<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Upload to PictShare</title>
  </head>
  <body>
    <input type="file" id="fileInput" accept="image/*" />
    <button onclick="uploadImage()">Upload</button>

    <script>
      async function uploadImage() {
        const fileInput = document.getElementById("fileInput");
        const file = fileInput.files[0];

        if (!file) {
          alert("Please select an image file.");
          return;
        }

        const formdata = new FormData();
        formdata.append("file", file);

        try {
          // const response = await fetch(
          //   "http://103.229.124.122:4001/api/entry?expiration=2025-02-08T17%3A00%3A21.000Z",
          //   {
          //     method: "POST",
          //     body: formData,
          //     headers: {
          //       "Content-Type": "multipart/form-data;",
          //       Cookie:
          //         "sharedSecret=a5z6cnSf/EQbo6shcLO7wLZ7dzUjf6J3AtXlbPay4Co=",
          //       // 如果PictShare需要额外的认证头，比如一个API密钥，
          //       // 你可以在这里添加它。但是根据你提供的docker命令，看起来不需要。
          //       // 'Authorization': 'Bearer YOUR_API_KEY',
          //     },
          //   }
          // );

          var myHeaders = new Headers();
          myHeaders.append(
            "Cookie",
            "sharedSecret=a5z6cnSf/EQbo6shcLO7wLZ7dzUjf6J3AtXlbPay4Co="
          );
          myHeaders.append("User-Agent", "Apifox/1.0.0 (https://apifox.com)");
          myHeaders.append("Accept", "*/*");
          myHeaders.append("Host", "103.229.124.122:4001");
          myHeaders.append("Connection", "keep-alive");
          myHeaders.append(
            "Content-Type",
            "multipart/form-data; boundary=--------------------------604773462374848634322636"
          );

          // var formdata = new FormData();
          // formdata.append(
          //   "file",
          //   fileInput.files[0],
          //   "C:\Users\work\Pictures\1.jpg"
          // );

          var requestOptions = {
            method: "POST",
            headers: myHeaders,
            body: formdata,
            redirect: "follow",
          };

          const response = await fetch(
            "http://103.229.124.122:4001/api/entry?expiration=2025-02-08T17%3A00%3A21.000Z",
            requestOptions
          )
            .then((response) => response.text())
            .then((result) => console.log(result))
            .catch((error) => console.log("error", error));

          if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
          }

          const data = await response.json();
          console.log("Success:", data);
          // 你可以在这里处理上传成功后的逻辑，比如显示图片URL。
        } catch (error) {
          console.error("Error uploading image:", error);
          alert("Failed to upload image. Please try again.");
        }
      }
    </script>
  </body>
</html>
